import scrapy
from robot.items import Book


class AllitebookSpider(scrapy.Spider):
    name = 'allitebook'

    start_urls = ['http://www.allitebooks.com/page/755/']

    def parse(self, response):
        for href in response.css('.entry-title a::attr(href)'):
            yield response.follow(href, self.parse_book)

        page = int(response.css('.current::text').extract_first())
        if page > 1:
            prev_page = 'http://www.allitebooks.com/page/%d/' % (page - 1)
            yield response.follow(prev_page, self.parse)

    def _parse_book_slug(self, url):
        return url.replace('http://www.allitebooks.com/','')[:-1]

    def parse_book(self, res):
        dd_list = res.xpath('//div[@class="book-detail"]/dl/dd')
        sub_title = res.css('h4::text')
        yield Book({
            'title': res.css('.single-title::text').extract_first().strip(),
            'sub_title': sub_title.extract_first().strip() if sub_title else None,
            'summary': res.xpath('//div[@class="entry-content"]').extract_first(),
            'author': dd_list[0].xpath('a/text()').extract(),
            'isbn': dd_list[1].xpath('text()').extract_first().strip(),
            'year': dd_list[2].xpath('text()').extract_first().strip(),
            'page_cnt': dd_list[3].xpath('text()').extract_first().strip(),
            'language': dd_list[4].xpath('text()').extract_first().strip(),
            'file_size': dd_list[5].xpath('text()').extract_first().strip(),
            'file_format': dd_list[6].xpath('text()').extract_first().strip(),
            'category': dd_list[7].xpath('a/@href').extract(),
            'download_url': res.xpath('//span[@class="download-links"]/a/@href').extract_first(),
            'source_url': res.url,
            'cover_url': res.xpath('//img[@class="attachment-post-thumbnail wp-post-image"]/@src').extract_first(),
            'slug': self._parse_book_slug(res.url)
        })
